//
//  BDMallViewController.m
//  BadBoy
//
//  Created by wen on 2017/4/14.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDMyViewController.h"
#import "BDUserModel.h"
#import "BDMallCollectionViewCell.h"
#import "UIButton+WebCache.h"
#import "BDGoumaiJiluViewController.h"
#import "BDGeneralCell.h"
#import "BDLoginViewController.h"
#import "BDHomeUserViewController.h"
#import "BDRedWhiteViewController.h"
#import "BDVIPViewController.h"
#import "BDMyOrderViewController.h"
#import "BDMyCouponsListViewController.h"
#import "ThemeViewController.h"
#import "BDGuanzhuShopViewController.h"
#import "BDMyClienteleViewController.h"
#import "BDSetViewController.h"
#import "BDMallViewController.h"
#import "BDMyDongTaiViewController.h"
#import "BDMyWalletViewController.h"
#import "BDMyGoldViewController.h"
#import "BDMyLevelViewController.h"
#import "JJPhotoManeger.h"


@interface BDMyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *datasourceArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *customView;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, assign) NSInteger activityType;
@property (nonatomic, strong) NSString *shareLogo;
@end

@implementation BDMyViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    NSString *url = [NSString stringWithFormat:@"%@tool/statisticsData", KBDBaseURL];
    NSString *headValid = [BDUserModel getHeaderValidation];
    RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
    [signal subscribeNext:^(id  _Nullable x) {
        self.activityType = [x[@"activity"][@"i_want_cash"][@"flag"] integerValue];
        self.shareLogo = x[@"activity"][@"i_want_cash"][@"logo"];
    }];
    
    BDUserModel *user = [BDUserModel getCurrentUser];
    if (user.is_guest == NO) {
        NSString *url = [NSString stringWithFormat:@"%@users/%@", KBDBaseURL, @(user.user_id)];
        NSString *headValid = [BDUserModel getHeaderValidation];
        RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
        [signal subscribeNext:^(id  _Nullable x) {
            BDUserModel *newUser = [BDUserModel mj_objectWithKeyValues:x];
            [newUser saveCurrentUser];
            self.tableView.tableHeaderView = [self cusView];
            [self requestDatasource];
            [self.tableView reloadData];
        }];
    }else{
        self.tableView.tableHeaderView = [self cusView];
        [self requestDatasource];
        [self.tableView reloadData];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //[self requestData];
    
    self.buttonArray = [NSMutableArray array];
    
    self.array = @[ASLocalizedString(@"余额"),ASLocalizedString(@"关注"),ASLocalizedString(@"帖子")];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, kBDScreenHeight - kBDTabBarHeight)];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = [self cusView];
    self.tableView.tableFooterView = [[UIView alloc] init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    
}

- (void)requestDatasource{
    self.datasourceArray = [NSMutableArray array];
    
    NSArray *firstArray = @[[BDGeneralModel new].titleEqual(ASLocalizedString(@"我的动态")).imageEqual(@"dongtai").accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator)];
    
    NSArray *secondArray = @[[BDGeneralModel new].titleEqual(ASLocalizedString(@"我的红包")).imageEqual(@"myhongbao").accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator),[BDGeneralModel new].titleEqual(ASLocalizedString(@"我的套餐")).imageEqual(@"taocan").accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator),[BDGeneralModel new].titleEqual(ASLocalizedString(@"我的订单")).imageEqual(@"dingdan").accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator)];
    
    NSMutableArray *threeArray = @[[BDGeneralModel new].titleEqual(ASLocalizedString(@"解锁内容")).imageEqual(@"jieshuo").accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator),[BDGeneralModel new].titleEqual(ASLocalizedString(@"我的特权")).imageEqual(@"tequan").accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator)].mutableCopy;
    
    BDUserModel *user= [BDUserModel getCurrentUser];
    if (user.vip_level >=2) {
        [threeArray addObject:[BDGeneralModel new].titleEqual(ASLocalizedString(@"我的客户")).imageEqual(@"tuiguang").accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator)];
    }
    if (self.activityType == 1 && user.is_guest != YES) {
        [threeArray addObject:[BDGeneralModel new].titleEqual(ASLocalizedString(@"我要现金")).imageEqual(@"xianjin").aimageStringEqual(@"hot").modeEqual(BDGeneralCellModeRightImage).accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator)];
    }
    
    [threeArray addObject:[BDGeneralModel new].titleEqual(ASLocalizedString(@"我的等级")).imageEqual(@"dengji").accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator)];
    
    NSArray *fourArray = @[[BDGeneralModel new].titleEqual(ASLocalizedString(@"我的商城")).imageEqual(@"shangcheng").accessoryTypeTypeEqual(BDGeneralCellAccessoryTypeDisclosureIndicator)];
    
    [self.datasourceArray addObject:firstArray];
    [self.datasourceArray addObject:secondArray];
    [self.datasourceArray addObject:threeArray];
    [self.datasourceArray addObject:fourArray];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datasourceArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.datasourceArray[section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BDGeneralModel *model = self.datasourceArray[indexPath.section][indexPath.row];
    model.hasSeparator = NO;
    BDGeneralCell *cell = [BDGeneralCell cellForTableView:self.tableView model:model indexPath:indexPath identifier:@"myCELL"];
    
    cell.subviewBlock = ^(BDGeneralCell *generalCEll){
        generalCEll.imageView.left = 10;
        generalCEll.imageView.top = 10;
        generalCEll.imageView.size = CGSizeMake(20, 20);
        
        generalCEll.textLabel.left = generalCEll.imageView.right + 20;
        generalCEll.textLabel.font = [UIFont systemFontOfSize:15];
        generalCEll.textLabel.textColor = [UIColor colorWithHexString:@"1c1c1c"];
        generalCEll.textLabel.top = 10;
        [generalCEll.textLabel sizeToFit];
        
        generalCEll.aImageView.left = generalCEll.textLabel.right - 5;
        generalCEll.aImageView.top = 2;
        generalCEll.aImageView.size = CGSizeMake(20, 15);
    };
    
    
    return cell;
}

-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BDUserModel *user = [BDUserModel getCurrentUser];
    if (user.is_guest == YES && !(indexPath.section == 2 && indexPath.row == 1)) {
        BDLoginViewController *loginVC = [[BDLoginViewController alloc] init];
        [self presentViewController:[[UINavigationController alloc] initWithRootViewController:loginVC] animated:YES completion:nil];
    }else{
        UIViewController *nextVC = nil;
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                nextVC = [[BDMyDongTaiViewController alloc] init];
            }
        }else if (indexPath.section == 1) {
            if (indexPath.row == 0) {
                //红包
                nextVC = [[BDRedWhiteViewController alloc] init];
            }else if (indexPath.row == 1){
                //我的套餐
                nextVC = [[BDMyCouponsListViewController alloc] init];
            }else if (indexPath.row == 2){
                //订单
                nextVC = [[BDMyOrderViewController alloc] init];
            }
        }else if (indexPath.section == 2){
            BDUserModel *user= [BDUserModel getCurrentUser];
            if (indexPath.row == 0){
                // 解锁内容
                nextVC = [[ThemeViewController alloc] init];
            }else if (indexPath.row == 1){
                //特权
                nextVC = [[BDVIPViewController alloc] init];
            }else if (user.vip_level >=2){
                if (indexPath.row == 2){
                    // 我的推广
                    nextVC = [[BDMyClienteleViewController alloc] init];
                }else if (indexPath.row == 3){
                    //  我要现金
                    nextVC = [[BDMyGoldViewController alloc] init];
                    ((BDMyGoldViewController *)nextVC).shareUrl = self.shareLogo;
                }else if (indexPath.row == 4){
                    //  我的等级
                    nextVC = [[BDMyLevelViewController alloc] init];
                }
            }else{
                if (indexPath.row == 2){
                    //  我要现金
                    nextVC = [[BDMyGoldViewController alloc] init];
                }else if (indexPath.row == 3){
                    //  我的等级
                    nextVC = [[BDMyLevelViewController alloc] init];
                }
            }
        }else if (indexPath.section == 3){
            if (indexPath.row == 0) {
                //  我的商城
                nextVC = [[BDMallViewController alloc] init];
            }
        }
        if (nextVC != nil){
            nextVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:nextVC animated:YES];
        }
        
    }
}
-(UIView *)cusView{
    BDUserModel *user = [BDUserModel getCurrentUser];
    if ([self.customView subviews].count != 0) {
        [self.customView removeAllSubviews];
    }
    self.customView = [[UIView alloc] init];
    self.customView.width = kBDScreenWidth;
    self.customView.left = 0;
    self.customView.top = 0;
    self.customView.height = 123 + 90;
    self.customView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *bgImageView = [[UIImageView alloc] init];
    bgImageView.left = 0;
    bgImageView.top = 0;
    bgImageView.size = CGSizeMake(kBDScreenWidth, 123);
    bgImageView.image = [UIImage imageNamed:@"beijingtu"];
    [self.customView addSubview:bgImageView];
    
    UIButton *settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    settingButton.top = 30;
    settingButton.left = kBDScreenWidth - 10 - 30;
    settingButton.size = CGSizeMake(30, 30);
    [settingButton setImage:[UIImage imageNamed:@"shezhi"] forState:UIControlStateNormal];
    [settingButton addTarget:self action:@selector(settingAction) forControlEvents:UIControlEventTouchUpInside];
    [self.customView addSubview:settingButton];
    
    UIView *codeBackView = [[UIView alloc] init];
    codeBackView.left = bgImageView.right - 10 - 25;
    codeBackView.top = bgImageView.bottom + 10;
    codeBackView.size = CGSizeMake(0, 0);
    codeBackView.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:codeBackView];
    
    UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] init];
    [codeBackView addGestureRecognizer:tapgesture];
    
    UIButton *codeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    codeButton.top = codeBackView.top;
    codeButton.left = codeBackView.left;
    codeButton.size = CGSizeMake(25, 25);
    [codeButton setImage:[UIImage imageNamed:@"erweima"] forState:UIControlStateNormal];
    [self.customView addSubview:codeButton];
    
    
    UIView *erweimaView = [[UIView alloc] init];
    erweimaView.top = 0;
    erweimaView.left = 0;
    erweimaView.size = CGSizeMake(0, 0);
    erweimaView.backgroundColor = [UIColor whiteColor];
    [codeBackView addSubview:erweimaView];
    
    NSString *string = [NSString stringWithFormat:@"beboy://user_id/%zd",[BDUserModel getCurrentUser].user_id];
    
    // 4. 显示二维码
    UIImageView *codeImageView = [[UIImageView alloc] init];
    codeImageView.size = CGSizeMake(0, 0);
    codeImageView.left = 0;
    codeImageView.top = 0;
    [erweimaView addSubview:codeImageView];
    
    codeImageView.image = [self creatQRImageWithString:string];
    //    codeImageView.image = [self createNonInterpolatedUIImageFormCIImage:image withSize:kBDScreenWidth - 80 - 90];
    
    UILabel *contextlabel = [[UILabel alloc] init];
    contextlabel.top = 0;
    contextlabel.left = 0;
    contextlabel.size = CGSizeMake(0, 0);
    contextlabel.text = ASLocalizedString(@"扫一扫，添加我为好友");
    contextlabel.font = [UIFont systemFontOfSize:12];
    contextlabel.textColor = KBDColorBlue;
    contextlabel.textAlignment = NSTextAlignmentCenter;
    [erweimaView addSubview:contextlabel];
    
    
    [[codeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
            codeBackView.left = 0;
            codeBackView.top = 0;
            codeBackView.width = kBDScreenWidth;
            codeBackView.height = kBDScreenHeight;
            
            erweimaView.left = 40;
            erweimaView.top = 200;
            erweimaView.size = CGSizeMake(kBDScreenWidth - 80, kBDScreenWidth - 80);
            
            codeImageView.left = 45;
            codeImageView.top = 25;
            codeImageView.width = kBDScreenWidth - 80 - 90;
            codeImageView.height = kBDScreenWidth - 80 - 90;
            
            contextlabel.top = codeImageView.bottom + 25;
            [contextlabel sizeToFit];
            contextlabel.left = (erweimaView.width - contextlabel.width) / 2;
            
            codeButton.hidden = YES;
        }];
    }];
    
    [[tapgesture rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
            codeBackView.left = bgImageView.right - 10 - 25;
            codeBackView.top = bgImageView.bottom + 10;
            codeBackView.size = CGSizeMake(0, 0);
            
            erweimaView.left = 0;
            erweimaView.top = 0;
            erweimaView.size = CGSizeMake(0, 0);
            
            codeImageView.width = 0;
            codeImageView.height = 0;
            codeImageView.left = 0;
            codeImageView.top = 0;
            
            contextlabel.top = 0;
            contextlabel.left = 0;
            contextlabel.size = CGSizeZero;
            codeButton.hidden = NO;
        }];
    }];
    
    
    UIImageView *avatorButton = [[UIImageView alloc] init];
    avatorButton.backgroundColor = [UIColor whiteColor];
    avatorButton.left = 10;
    avatorButton.top = bgImageView.bottom - 32;
    avatorButton.size = CGSizeMake(64, 64);
    avatorButton.layer.cornerRadius = 64 / 2;
    avatorButton.layer.borderWidth = 2;
    avatorButton.layer.borderColor = [UIColor whiteColor].CGColor;
    avatorButton.layer.masksToBounds = YES;
    [avatorButton sd_setImageWithURL:[NSURL URLWithString:user.cover] placeholderImage:[UIImage imageNamed:@"avatar"]];
    avatorButton.userInteractionEnabled = YES;
    UITapGestureRecognizer *avatorTap = [[UITapGestureRecognizer alloc] init];
    [avatorButton addGestureRecognizer:avatorTap];
    
    [[avatorTap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [BDUserModel judgeCurrentUserWithNormalBlock:^{
            BDSetViewController *setVc = [[BDSetViewController alloc]init];
            setVc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setVc animated:YES];
        }];
    }];
    [self.customView addSubview:avatorButton];
    
    
    UILabel *sexLabel = [[UILabel alloc] init];
    sexLabel.left = avatorButton.right + 14;
    sexLabel.textColor = [UIColor whiteColor];
    sexLabel.font = [UIFont systemFontOfSize:12];
    if (user.sex == 1) {
        sexLabel.text = ASLocalizedString(@"男");
    }else if (user.sex == 2){
        sexLabel.text = ASLocalizedString(@"女");
    }
    [sexLabel sizeToFit];
    sexLabel.bottom = bgImageView.bottom - 10;
    [self.customView addSubview:sexLabel];
    
    UIImageView *sexImageView = [[UIImageView alloc] init];
    sexImageView.left = sexLabel.right + 5;
    sexImageView.top = sexLabel.top;
    sexImageView.size = CGSizeMake(15, 15);
    if (user.sex == 1) {
        sexImageView.image = [UIImage imageNamed:@"nan"];
    }else if (user.sex == 2){
        sexImageView.image = [UIImage imageNamed:@"nv"];
    }
    [self.customView addSubview:sexImageView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.left = sexLabel.left;
    titleLabel.top = bgImageView.bottom + 10;
    titleLabel.textColor = [UIColor colorWithHexString:@"333333"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.text = user.nickname;;
    [titleLabel sizeToFit];
    [self.customView addSubview:titleLabel];
    
    UIImageView *levelImageView = [[UIImageView alloc] init];
    levelImageView.left = titleLabel.right + 5;
    levelImageView.size = CGSizeMake(22 / 2, 26 / 2);
    levelImageView.centerY = titleLabel.centerY;
    levelImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"leve%ld",[user.points[@"level"] integerValue]]];
    [self.customView addSubview:levelImageView];
    
    UIImageView *vipImageView = [[UIImageView alloc] init];
    vipImageView.left = levelImageView.right + 5;
    vipImageView.size = CGSizeMake(34 / 2, 25 / 2);
    vipImageView.centerY = levelImageView.centerY;
    vipImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"showVip%ld",user.vip_level]];
    [self.customView addSubview:vipImageView];
    
    for (int i = 0; i < 3; i++) {
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth / 3, 12)];
        textLabel.text = self.array[i];
        textLabel.textColor = [UIColor colorWithHexString:@"333333"];
        textLabel.font = [UIFont systemFontOfSize:12];
        textLabel.textAlignment = NSTextAlignmentCenter;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.top = avatorButton.bottom + 20;
        button.left = i * (kBDScreenWidth / 3);
        button.size = CGSizeMake(kBDScreenWidth / 3, 85 - 32 - 20);
        [button setImage:[UIImage convertViewToImage:textLabel] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"4d7aad"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag = 300 + i;
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        if (i == 0) {
            [button setTitle:[NSString stringWithFormat:@"%.2f",user.balance] forState:UIControlStateNormal];
        }else if (i == 1){
            [button setTitle:[NSString stringWithFormat:@"%ld",user.shop_fav] forState:UIControlStateNormal];
        }else{
            [button setTitle:[NSString stringWithFormat:@"%ld",user.posts] forState:UIControlStateNormal];
        }
        
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [BDUserModel judgeCurrentUserWithNormalBlock:^{
                if (i == 0) {
                    
                    BDMyWalletViewController *moneyVC = [[BDMyWalletViewController alloc] init];
                    moneyVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:moneyVC animated:YES];
                    
                }else if (i == 1){
                    //关注商家
                    BDGuanzhuShopViewController *nextVC = [[BDGuanzhuShopViewController alloc] init];
                    nextVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:nextVC animated:YES];
                }else{
                    BDHomeUserViewController *homeUserVC = [[BDHomeUserViewController alloc] init];
                    homeUserVC.user = user;
                    homeUserVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:homeUserVC animated:YES];
                }
            }];
        }];
        
        button.titleFrame = [NSValue valueWithCGRect:CGRectMake(0, 0, kBDScreenWidth / 3, 14)];
        button.imageFrame = [NSValue valueWithCGRect:CGRectMake(0, 20, kBDScreenWidth / 3, 12)];
        
        [self.buttonArray addObject:button];
        [self.customView addSubview:button];
        
        if (i < 2) {
            UIView *lineView = [[UIView alloc] init];
            lineView.left = (i + 1) * (kBDScreenWidth / 3);
            lineView.top = button.top + 10;
            lineView.size = CGSizeMake(1, button.height - 20);
            lineView.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
            [self.customView addSubview:lineView];
        }
    }
    
    if (user.is_guest == YES) {
        sexLabel.hidden = YES;
        sexImageView.hidden = YES;
        
        for (int i = 0; i < self.buttonArray.count; i++) {
            if (i == 0) {
                UIButton *btn = [self.view viewWithTag:300 + i];
                [btn setTitle:@"0.00" forState:UIControlStateNormal];
            }else{
                UIButton *btn = [self.view viewWithTag:300 + i];
                [btn setTitle:@"0" forState:UIControlStateNormal];;
            }
        }
    }
    
    
    
    return self.customView;
}

- (UIImage *)creatQRImageWithString:(NSString *)string{
    UIImage *qrImage = [UIImage qrImageForString:string imageSize:kBDScreenWidth - 80 - 90 logoImageSize:0];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth - 80 - 90, kBDScreenWidth - 80 - 90)];
    imageView.image = qrImage;
    
    UIView *litView = [[UIView alloc] init];
    litView.size = CGSizeMake(60, 60);
    litView.backgroundColor = [UIColor whiteColor];
    litView.center = CGPointMake(imageView.width / 2.0, imageView.height / 2.0);
    litView.layer.masksToBounds = YES;
    litView.layer.cornerRadius = 5;
    [imageView addSubview:litView];
    
    UIImageView *litImageView = [[UIImageView alloc] init];
    litImageView.size = CGSizeMake(litView.width - 4, litView.width - 4);
    litImageView.center = CGPointMake(litView.width / 2.0, litView.height / 2.0);
    litImageView.layer.masksToBounds = YES;
    litImageView.layer.cornerRadius = 3;
    [litImageView sd_setImageWithURL:[NSURL URLWithString:[BDUserModel getCurrentUser].cover] placeholderImage:[UIImage imageNamed:@"avatar"]];
    [litView addSubview:litImageView];
    
    return [UIImage convertViewToImage:imageView];
}

-(void)settingAction{
    [BDUserModel judgeCurrentUserWithNormalBlock:^{
        UIViewController *nextVC = nil;
        nextVC = [[BDSetViewController alloc]init];
        if (nextVC != nil){
            nextVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:nextVC animated:YES];
        }
    }];
}
/*
 - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
 
 CGRect extent = CGRectIntegral(image.extent);
 CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
 
 // 1.创建bitmap;
 size_t width = CGRectGetWidth(extent) * scale;
 size_t height = CGRectGetHeight(extent) * scale;
 CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
 CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
 CIContext *context = [CIContext contextWithOptions:nil];
 CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
 CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
 CGContextScaleCTM(bitmapRef, scale, scale);
 CGContextDrawImage(bitmapRef, extent, bitmapImage);
 
 // 2.保存bitmap到图片
 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
 CGContextRelease(bitmapRef);
 CGImageRelease(bitmapImage);
 
 //原图
 UIImage *outputImage = [UIImage imageWithCGImage:scaledImage];
 //给二维码加 logo 图
 UIGraphicsBeginImageContextWithOptions(outputImage.size, NO, [[UIScreen mainScreen] scale]);
 
 
 [outputImage drawInRect:CGRectMake(0,0 , size, size)];
 //logo图
 UIImage *waterimage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[BDUserModel getCurrentUser].cover]]];
 
 //把logo图画到生成的二维码图片上，注意尺寸不要太大（最大不超过二维码图片的%30），太大会造成扫不出来
 [waterimage drawInRect:CGRectMake((size-60)/2.0, (size-60)/2.0, 60, 60)];
 
 
 UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 
 return newPic;
 }
 */
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
